package com.ysy.tz.core.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import java.io.File;
import javafx.application.Platform;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.FileChooser;

public abstract class ExcelOutHandler {
    int pageNum = 1;
    int pageSize = 2000;
    public static volatile Thread singleWorkerThread;

    public ExcelOutHandler() {
    }

    public void setSegmentStrategy(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public abstract ExcelData exportSegment(int var1, int var2);

    public void start() {
        if (singleWorkerThread != null) {
            (new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("do_excel"), new ButtonType[0])).showAndWait();
        } else {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle(ApplicationContext.commonBundle.getString("excel_out"));
            File file = fileChooser.showSaveDialog(ApplicationContext.mainStage);
            if (file != null) {
                final String path = file.getPath().indexOf("xls") == -1 ? file.getPath() + ".xls" : file.getPath();
                ApplicationContext.mainPage.getExcelPane().setVisible(true);
                singleWorkerThread = new Thread(new Runnable() {
                    public void run() {
                        ExcelData excelData = ExcelOutHandler.this.exportSegment(ExcelOutHandler.this.pageNum, ExcelOutHandler.this.pageSize);
                        if (excelData != null) {
                            ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(path);
                            ExcelWriter excelWriter = ((ExcelWriterBuilder)((ExcelWriterBuilder)excelWriterBuilder.head(excelData.getHeaders())).registerWriteHandler(new AutoHeadColumnWidthStyleStrategy())).build();
                            WriteSheet writeSheet = excelWriterBuilder.sheet(0).build();
                            excelWriter.write(excelData.getDatas(), writeSheet);
                            if (excelData.getTotal() > (long)ExcelOutHandler.this.pageSize) {
                                int totalPage = (int)Math.ceil((double)excelData.getTotal() * 1.0 / (double)ExcelOutHandler.this.pageSize);

                                for(int curPageNum = 2; curPageNum <= totalPage; ++curPageNum) {
                                    excelData = ExcelOutHandler.this.exportSegment(ExcelOutHandler.this.pageNum, ExcelOutHandler.this.pageSize);
                                    excelWriter.write(excelData.getDatas(), writeSheet);
                                    double percentage = (double)curPageNum * 1.0 / (double)totalPage;
                                    Platform.runLater(() -> {
                                        ApplicationContext.mainPage.getExcelPane().setProgress(percentage);
                                    });
                                }
                            }

                            ApplicationContext.mainPage.getExcelPane().setProgress(1.0);
                            excelWriter.finish();
                        } else {
                            Platform.runLater(() -> {
                                (new CustomerAlert(AlertType.WARNING, ApplicationContext.commonBundle.getString("no_excel_data"), new ButtonType[0])).showAndWait();
                            });
                        }

                        ApplicationContext.mainPage.getExcelPane().setVisible(false);
                        ApplicationContext.mainPage.getExcelPane().setProgress(0.0);
                        ExcelOutHandler.singleWorkerThread = null;
                    }
                });
                singleWorkerThread.start();
            }

        }
    }
}
